package com.vsked.classMssqls;

import java.io.File;
import java.io.FileWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class MssqlSon {
	private static Connection conn = null; // 返回的数据库连接对象
	private static ResultSet rs; // 行结果集
	private static ResultSetMetaData rm; // 列结果集
	private static String dbaddr = "localhost"; // 数据库连接url中的地址
	private static int dbport = 1433; // 数据库连接的端口号
	private static String databaseName = "pubs"; // 数据库名
	private static String dataSourceName = "test";// 数据源名
	private static String mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr
			+ ":" + dbport + ";databaseName=" + databaseName; // 数据库连接的url
	private static String uname = "sa"; // 数据库用户名
	private static String upwd = "sa"; // 数据库用户密码
	private static String drivestr = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; // 数据库驱动字符串

	private static boolean flag = false;// 成功失败标志

	public static final int MSSQL_SERVER_PORT = 1433; // sqlserver默认端口号
	public static final int ORACLE_ORACLE_PORT = 1521;// oracle默认端口号码

	public static final String MSSQL_SERVER_DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; // sqlserver的默认驱动
	public static final String ORACLE_ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver"; // oracle默认驱动

	private static Vector vt = new Vector(); // 存储查询结果的Vector
	private static ArrayList al = new ArrayList();// 存储查询结果的ArrayList

	private static String sql;// 要执行的sql语句
	private static Statement st;
	private static PreparedStatement pt;
	private static CallableStatement ct;

	private static String tablename; // 要查询的表名
	private static String[] tablenames;// 数据库内所有表名
	private static int tablecount = 0; // 数据库内表的总数
	private static int tablecolumncount = 0; // 表格内总列数..
	private static int tablerowcount = 0; // 表格内总行数
	private static String[] tablecolumnnames;// 表格内某一行数据所有列..
	private static String[] tablerowdatas; // 表格内某列数据所有行..汗..
	private static String columnname;// 列名

	// ------------------------------------------------------------
	/**
	 * 得到当前正在使用数据库驱动的字符串
	 * 
	 * @return String driverstr
	 */
	public static String getDrivestr() {
		return drivestr;
	}

	/**
	 * 设置数据库的驱动
	 * 
	 * @param String
	 *            drivestr
	 */
	public static void setDrivestr(String drivestr) {
		MssqlSon.drivestr = drivestr;
	}

	/**
	 * 设置数据库的连接对象conn
	 */
	public static void setConnection(Connection conn) {
		MssqlSon.conn = conn;
	}

	/**
	 * 返回数据库连接对象conn
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		return conn==null?MssqlSon.connectDB():conn;
	}

	/**
	 * 得到当前正在使用的列名 只对提取单列所有数据时有效果..汗.
	 * 
	 * @return String columnname
	 */
	public static String getColumnname() {
		return columnname;
	}

	/**
	 * 设置将要使用的列名 对提取单列所有数据时有效..汗..
	 * 
	 * @param String
	 *            columnname
	 */
	public static void setColumnname(String columnname) {
		MssqlSon.columnname = columnname;
	}

	/**
	 * 设置将要使用的数据库名
	 * 
	 * @param String
	 *            databaseName
	 */
	public static void setDatabaseName(String databaseName) {
		MssqlSon.databaseName = databaseName;
	}

	/**
	 * 得到当前正在使用的数据库名
	 * 
	 * @return String DatabaseName
	 */
	public static String getDatabaseName() {
		return databaseName;
	}

	/**
	 * 得到当前正在使用的表名
	 * 
	 * @return String Tablename
	 */
	public static String getTablename() {
		return tablename;
	}

	/**
	 * 设置要使用的表名
	 * 
	 * @param tablename
	 */
	public static void setTablename(String tablename) {
		MssqlSon.tablename = tablename;
	}

	/**
	 * 得到正在使用的数据库服务器地址
	 * 
	 * @return String Dbaddr
	 */
	public static String getDbaddr() {
		return dbaddr;
	}

	/**
	 * 设置要使用的数据库服务器的地址
	 * 
	 * @param String
	 *            dbaddr
	 */
	public static void setDbaddr(String dbaddr) {
		MssqlSon.dbaddr = dbaddr;
	}

	/**
	 * 得到正在使用的数据库的端口号
	 * 
	 * @return int Dbport
	 */
	public static int getDbport() {
		return dbport;
	}

	/**
	 * 设置要使用的数据库的端口号
	 * 
	 * @param int dbport
	 */
	public static void setDbport(int dbport) {
		MssqlSon.dbport = dbport;
	}

	/**
	 * 得到当前连接数据库所使用的url
	 * 
	 * @return String url
	 */
	public static String getMssqlurl() {
		return mssqlurl;
	}

	/**
	 * 设置数据库将要使用的url
	 * 
	 * @param String
	 *            mssqlurl
	 */
	public static void setMssqlurl(String mssqlurl) {
		MssqlSon.mssqlurl = mssqlurl;
	}

	/**
	 * 得到当前正在使用的数据库用户名和密码用逗号分隔
	 * 
	 * @return String uname,String upwd
	 */
	public static String getUnameAndUpwd() {
		return uname + "," + upwd;
	}

	/**
	 * 设置数据库连接将要使用的用户名和密码
	 * 
	 * @param String
	 *            uname
	 * @param String
	 *            upwd
	 */
	public static void setUnameAndUpwd(String uname, String upwd) {
		MssqlSon.uname = uname;
		MssqlSon.upwd = upwd;
	}

	/**
	 * 得到当前正在使用的数据库用户名
	 * 
	 * @return String Uname
	 */
	public static String getUname() {
		return uname;
	}

	/**
	 * 设置将要使用的数据库用户名
	 * 
	 * @param String
	 *            uname
	 */
	public static void setUname(String uname) {
		MssqlSon.uname = uname;
	}

	/**
	 * 得到当前数据库用户密码
	 * 
	 * @return String Upwd
	 */
	public static String getUpwd() {
		return upwd;
	}

	/**
	 * 设置将要使用的数据库用户的密码
	 * 
	 * @param String
	 *            upwd
	 */
	public static void setUpwd(String upwd) {
		MssqlSon.upwd = upwd;
	}

	/**
	 * 得到当前正在执行的sql语句
	 * 
	 * @return String sql
	 */
	public static String getSql() {
		return sql;
	}

	/**
	 * 设置将要执行的sql语句
	 * 
	 * @param String
	 *            sql
	 */
	public static void setSql(String sql) {
		MssqlSon.sql = sql;
	}

	// -------------------------------------------------------------------

	/**
	 * 默认得到连接数据库名pubs端口1433用户名sa密码sa
	 * 
	 * @return
	 */
	public static Connection connectDB() {
		mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr + ":" + dbport
				+ ";databaseName=" + databaseName;
		try {
			Class.forName(drivestr);
			conn = DriverManager.getConnection(mssqlurl, uname, upwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}// end method connectDB()

	/**
	 * 连接其他数据库时连接 使用前请设置数据库驱动(driverstr)地址(mssqlurl)连接的用户名(uname) 密码(upwd)
	 * 
	 * @return
	 */
	public static Connection connectDBo() {
		try {
			Class.forName(drivestr);
			conn = DriverManager.getConnection(mssqlurl, uname, upwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}// end method connectDB()

	/**
	 * ODBC连接数据库方式
	 * 
	 * @return Connection conn
	 */
	public static Connection connectDBbyODBC(String dataSourceName) {
		if (dataSourceName != null) {
			try {
				MssqlSon.setDrivestr("sun.jdbc.odbc.JdbcOdbcDriver");
				Class.forName(drivestr);
				mssqlurl = "jdbc:odbc:" + dataSourceName;
				conn = DriverManager.getConnection(mssqlurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}
	}// end method connectDB()

	/**
	 * 传入数据库名得到数据库连接
	 * 
	 * @param String
	 *            databaseName
	 * @return Connection or null
	 */
	public static Connection connectDB(String databaseName) {
		if (databaseName != null) {
			mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr + ":" + dbport
					+ ";databaseName=" + databaseName;
			try {
				Class.forName(drivestr);
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}

	}// end Method connectDB(String databaseName)

	/**
	 * 传入数据库地址 端口号 数据库名 用户名 密码 返回一个sqlserver的连接对象
	 * 
	 * @param String
	 *            dbaddr数据库地址
	 * @param String
	 *            dbport 端口号
	 * @param String
	 *            databaseName数据库名
	 * @param String
	 *            uname用户名
	 * @param String
	 *            upwd密码
	 * @return Connection
	 */
	public static Connection connectDB(String dbaddr, int dbport,String databaseName, String uname, String upwd) {
		if (databaseName != null && uname != null) {
			mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr + ":" + dbport
					+ ";databaseName=" + databaseName;
			try {
				Class.forName(drivestr);
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}

	}

	/**
	 * 传入数据库 端口号 数据库名 用户名 密码 返回一个sqlserver的连接对象
	 * 
	 * @param String
	 *            dbport 端口号
	 * @param String
	 *            databaseName数据库名
	 * @param String
	 *            uname用户名
	 * @param String
	 *            upwd密码
	 * @return Connection
	 */
	public static Connection connectDB(int dbport, String databaseName,String uname, String upwd) {
		if (databaseName != null && uname != null) {
			mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr + ":" + dbport
					+ ";databaseName=" + databaseName;
			try {
				Class.forName(drivestr);
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}

	}

	/**
	 * 传入数据库名 数据库用户名 密码 得到数据库连接
	 * 
	 * @param String
	 *            databaseName 数据库名
	 * @param String
	 *            uname 数据库用户名
	 * @param String
	 *            upwd 数据库用户密码
	 * @return Connection
	 */
	public static Connection connectDB(String databaseName, String uname,String upwd) {
		if (databaseName != null && uname != null) {
			mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr + ":" + dbport
					+ ";databaseName=" + databaseName;
			try {
				Class.forName(drivestr);
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}

	}

	/**
	 * 传入数据库名 数据库用户名 密码 得到数据库连接
	 * 
	 * @param String
	 *            uname 数据库用户名
	 * @param String
	 *            upwd 数据库用户密码
	 * @return Connection
	 */
	public static Connection connectDB(String uname, String upwd) {
		if (databaseName != null && uname != null) {
			mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr + ":" + dbport
					+ ";databaseName=" + databaseName;
			try {
				Class.forName(drivestr);
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}

	}

	//--------------------------------------------------------------------------

	/**
	 * 得到oracle的thin连接对象
	 * 
	 * @param String
	 *            dbaddr 数据库地址
	 * @param String
	 *            dbport 数据库端口号
	 * @param String
	 *            databaseName 数据库实例名
	 * @param String
	 *            uname 用户名
	 * @param String
	 *            upwd 密码
	 * @return Connection
	 */
	public static Connection connectDBThinOracle(String dbaddr, int dbport,	String databaseName, String uname, String upwd) {
		if (dbaddr != null && dbport > 0 && databaseName != null
				&& uname != null) {
			mssqlurl = "jdbc:oracle:thin:@" + dbaddr + ":" + dbport + ":"
					+ databaseName;
			try {
				Class.forName(drivestr);
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}

	}

	/**
	 * 得到oracle的thin连接对象
	 * 
	 * @param String
	 *            dbport 数据库端口号
	 * @param String
	 *            databaseName 数据库实例名
	 * @param String
	 *            uname 用户名
	 * @param String
	 *            upwd 密码
	 * @return Connection
	 */
	public static Connection connectDBThinOracle(int dbport,String databaseName, String uname, String upwd) {
		if (dbaddr != null && dbport > 0 && databaseName != null
				&& uname != null) {
			mssqlurl = "jdbc:oracle:thin:@127.0.0.1:" + dbport + ":"
					+ databaseName;
			try {
				Class.forName(drivestr);
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}

	}

	/**
	 * 得到oracle的thin连接对象
	 * 
	 * @param String
	 *            databaseName 数据库实例名
	 * @param String
	 *            uname 用户名
	 * @param String
	 *            upwd 密码
	 * @return Connection
	 */
	public static Connection connectDBThinOracle(String databaseName,String uname, String upwd) {
		if (dbaddr != null && dbport > 0 && databaseName != null
				&& uname != null) {
			mssqlurl = "jdbc:oracle:thin:@127.0.0.1:1521:" + databaseName;
			try {
				Class.forName(drivestr);
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}

	}

	/**
	 * 得到oracle的thin连接对象
	 * 
	 * @param String
	 *            uname 用户名
	 * @param String
	 *            upwd 密码
	 * @return Connection
	 */
	public static Connection connectDBThinOracle(String uname, String upwd) {
		if (dbaddr != null && dbport > 0 && databaseName != null
				&& uname != null) {
			try {
				Class.forName(drivestr);
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}

	}

	/**
	 * 得到oracle的thin连接对象 使用前请调用各类相关set方法..呵呵
	 * 
	 * @return Connection
	 */
	public static Connection connectDBThinOracle() {
		if (dbaddr != null && dbport > 0 && databaseName != null
				&& uname != null) {
			try {
				Class.forName(drivestr);
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		} else {
			return null;
		}

	}

	//--------------------------------------------------------------------------
	// ------

	//--------------------------------------------------------------------------
	// ----------------
	// ------------------------------------------------------------------------

	/**
	 * 传入一条非查询sql语句 并执行 sql语句长度大于20
	 * 
	 * @param String
	 *            sqls
	 * @return boolean flag
	 */
	public static boolean excuteSqls(String sqls) {
		if (sqls != null) {
			if (sqls.length() > 20) {
				try {
					conn = MssqlSon.getConnection();
					st = conn.createStatement();
					int row = st.executeUpdate(sqls);
					if (row > 0) {
						flag = true;
					}
				} catch (Exception e) {
                     e.printStackTrace();
				} finally {
					MssqlSon.closeDB();
					return flag;
				}
			} else {
				JOptionPane.showMessageDialog(null,
						"要执行的sql语句有问题..请检查传入的sql语句!");
				return false;
			}
		} else {
			JOptionPane.showMessageDialog(null, "要执行的sql语句有问题..请检查传入的sql语句!");
			return false;
		}
	}

	/**
	 * 传入一条查询语句 返回封装好的Vector
	 * 
	 * @param String
	 *            sqls
	 * @return Vector vt
	 */
	public static Vector excuteQuerySqlsV(String sqls) {
		if (sqls != null) {
			if (sqls.length() >= 15) {
				try {
					conn = MssqlSon.getConnection();
					st = conn.createStatement();
					rs = st.executeQuery(sqls);
					vt = new Vector();
					while (rs.next()) {
						rm = rs.getMetaData();
						Vector va = new Vector();
						for (int i = 1; i <= rm.getColumnCount(); i++) {
							va.add(rs.getString(i));
						}
						vt.add(va);

					}
				} catch (Exception e) {
                       e.printStackTrace();
				} finally {
					MssqlSon.closeDB();
				}
				return vt;
			} else {
				JOptionPane.showMessageDialog(null, "你输入的查询语句可能有问题哟..呵呵");
				return null;
			}
		} else {
			JOptionPane.showMessageDialog(null, "你输入的查询语句可能有问题哟..呵呵");
			return null;

		}
	}

	/**
	 * 传入一条查询的sql语句 返回封装后的ArrayList
	 * 
	 * @param String
	 *            sqls
	 * @return ArrayList al
	 */
	public static ArrayList excuteQuerySqlsA(String sqls) {
		if (sqls != null) {
			if (sqls.length() >= 15) {
				try {
					conn = MssqlSon.getConnection();
					st = conn.createStatement();
					rs = st.executeQuery(sqls);
					al = new ArrayList();
					while (rs.next()) {
						rm = rs.getMetaData();
						ArrayList ta = new ArrayList();
						for (int i = 1; i <= rm.getColumnCount(); i++) {
							ta.add(rs.getString(i));
						}
						al.add(ta);

					}
				} catch (Exception e) {
                          e.printStackTrace();
				} finally {
					MssqlSon.closeDB();
				}
				return al;
			} else {
				JOptionPane.showMessageDialog(null, "你输入的查询语句可能有问题哟..呵呵");
				return null;
			}
		} else {
			JOptionPane.showMessageDialog(null, "你输入的查询语句可能有问题哟..呵呵");
			return null;
		}
	}

	//--------------------------------------------------------------------------

	/**
	 * 传入数据库名 返回数据库内表总数
	 * 
	 * @param String
	 *            databaseName
	 * @return return int tableCount
	 */
	public static int getTableCount(String databasename) {
		if (databasename == null) {
			JOptionPane.showMessageDialog(null, "传入的数据库名不能为空!");
			return 0;
		} else {
			mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr + ":" + dbport
					+ ";databaseName=" + databasename;
			try {
				Class.forName(drivestr);
			} catch (ClassNotFoundException e) {				
				JOptionPane
						.showMessageDialog(null, "数据库驱动丢失!请检查数据库驱动jar包有没有导入");
				e.printStackTrace();
				return 0;
			}
			try {
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
				pt = conn
						.prepareStatement("select count(name) from sysobjects where xtype='u'");
				rs = pt.executeQuery();
				if (rs.next()) {
					tablecount = rs.getInt(1);
					System.out.println(databasename + "数据库内共有用户表总数:"
							+ tablecount + "个");
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						"数据库地址数据库用户名密码可能有错!请检查数据库名是否输入正确!数据库用户名是否正确密码是否正确");
				e.printStackTrace();
				return 0;
			} finally {
				MssqlSon.closeDB();
			}
			return tablecount;
		}
	}

	/**
	 * 返回数据库内表总数 使用前请使用setDatabaseName()方法
	 * 
	 * @return return int tableCount
	 */
	public static int getTableCount() {
		if (databaseName == null) {
			JOptionPane.showMessageDialog(null, "传入的数据库名不能为空!");
			return 0;
		} else {
			mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr + ":" + dbport
					+ ";databaseName=" + databaseName;
			try {
				Class.forName(drivestr);
			} catch (ClassNotFoundException e) {
				JOptionPane
						.showMessageDialog(null, "数据库驱动丢失!请检查数据库驱动jar包有没有导入");
				e.printStackTrace();
				return 0;
			}
			try {
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
				pt = conn
						.prepareStatement("select count(name) from sysobjects where xtype='u'");
				rs = pt.executeQuery();
				if (rs.next()) {
					tablecount = rs.getInt(1);
					System.out.println(databaseName + "数据库内共有用户表总数:"
							+ tablecount + "个");
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						"数据库地址数据库用户名密码可能有错!请检查数据库名是否输入正确!数据库用户名是否正确密码是否正确");
				e.printStackTrace();
				return 0;
			} 
			return tablecount;
		}
	}
	
	/**
	 * 传入数据库名 得到数据库内所有表名封装成字符串数组
	 * 
	 * @param String
	 *            databasename
	 * @return String[]
	 */
	public static String[] getTableNames(String databasename) {
		if (databasename == null) {
			JOptionPane.showMessageDialog(null, "传入的数据库名不能为空!");
			return null;
		} else {
			mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr + ":" + dbport
					+ ";databaseName=" + databasename;
			try {
				Class.forName(drivestr);
			} catch (ClassNotFoundException e) {
				JOptionPane
						.showMessageDialog(null, "数据库驱动丢失!请检查数据库驱动jar包有没有导入");
				e.printStackTrace();
				return null;
			}
			try {
				int i = 0;
				tablenames = new String[MssqlSon.getTableCount(databasename)];
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
				pt = conn
						.prepareStatement("select name from sysobjects where xtype='u'");
				rs = pt.executeQuery();
				while (rs.next()) {
					tablenames[i] = rs.getString(1);
					System.out.println(databasename + "数据库内用户表分别是:"
							+ tablenames[i]);
					i++;
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						"数据库地址数据库用户名密码可能有错!请检查数据库名是否输入正确!数据库用户名是否正确密码是否正确");
				e.printStackTrace();
				return null;
			} 
			return tablenames;
		}
	}

	/**
	 * 得到数据库内所有表名封装成字符串数组 使用前请使用setdatabaseName()方法
	 * 
	 * @return String[]
	 */
	public static String[] getTableNames() {
		if (databaseName == null) {
			JOptionPane.showMessageDialog(null, "传入的数据库名不能为空!");
			return null;
		} else {
			mssqlurl = "jdbc:microsoft:sqlserver://" + dbaddr + ":" + dbport
					+ ";databaseName=" + databaseName;
			try {
				Class.forName(drivestr);
			} catch (ClassNotFoundException e) {
				JOptionPane
						.showMessageDialog(null, "数据库驱动丢失!请检查数据库驱动jar包有没有导入");
				e.printStackTrace();
				return null;
			}
			try {
				int i = 0;
				tablenames = new String[MssqlSon.getTableCount(databaseName)];
				conn = DriverManager.getConnection(mssqlurl, uname, upwd);
				pt = conn
						.prepareStatement("select name from sysobjects where xtype='u'");
				rs = pt.executeQuery();
				while (rs.next()) {
					tablenames[i] = rs.getString(1);
					System.out.println(databaseName + "数据库内用户表分别是:"
							+ tablenames[i]);
					i++;
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						"数据库地址数据库用户名密码可能有错!请检查数据库名是否输入正确!数据库用户名是否正确密码是否正确");
				e.printStackTrace();
				return null;
			}
			return tablenames;
		}
	}



	/**
	 * 传入表名得到表内列总数
	 * 
	 * @param String
	 *            tablename
	 * @return int tablecolumncount
	 */
	public static int getTableColumnCount(String tablename) {
		if (tablename != null) {
			if (tablename.length() <= 0) {
				JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
				return 0;
			} else {
				try {
					conn = MssqlSon.getConnection();
					pt = conn.prepareStatement("select count(*) from syscolumns where id=(select id from sysobjects where xtype='u' and name='"+tablename+"')");
					rs = pt.executeQuery();
					if (rs.next()) {
						tablecolumncount = rs.getInt(0);
					}
				} catch (Exception e) {
                        e.printStackTrace();
				} 
				return tablecolumncount;
			}
		} else {
			JOptionPane.showMessageDialog(null, "没有表名表名哪儿去了..汗");
			return 0;
		}
	}

	/**
	 * 得到表内列总数 使用前请先调用setTablename()方法
	 * 
	 * @return int tablecolumncount
	 */
	public static int getTableColumnCount() {
		if (tablename != null) {
			if (tablename.length() <= 0) {
				JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
				return 0;
			} else {
				try {
					conn = MssqlSon.getConnection();
					pt = conn.prepareStatement("select count(*) from syscolumns where id=(select id from sysobjects where xtype='u' and name='"+tablename+"')");
					rs = pt.executeQuery();
					if (rs.next()) {
						tablecolumncount = rs.getInt(1);
					}
				} catch (Exception e) {
                         e.printStackTrace();
				}
				return tablecolumncount;
			}
		} else {
			JOptionPane.showMessageDialog(null, "没表名啊..看看吧");
			return 0;
		}
	}

	/**
	 * 返回表内行总数 使用前请先调用settablename()方法
	 * 
	 * @return int rowcount
	 */
	public static int getTableRowCount() {
		if (tablename != null) {
			if (tablename.length() <= 0) {
				JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
				return 0;
			} else {
				try {
					conn = MssqlSon.getConnection();
					pt = conn.prepareStatement("select count(*) from "
							+ tablename);
					rs = pt.executeQuery();
					if (rs.next()) {
						tablerowcount = rs.getInt(1);
					}
				} catch (Exception e) {
                         e.printStackTrace();
				} 
				return tablerowcount;
			}
		} else {
			JOptionPane.showMessageDialog(null, "没有表名..汗..");
			return 0;
		}
	}

	/**
	 * 传入表名 返回表内行总数
	 * 
	 * @param String
	 *            tablename
	 * @return int rowcount
	 */
	public static int getTableRowCount(String tablename) {
		if (tablename != null) {
			if (tablename.length() <= 0) {
				JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
				return 0;
			} else {
				try {
					conn = MssqlSon.getConnection();
					pt = conn.prepareStatement("select count(*) from "
							+ tablename);
					rs = pt.executeQuery();
					if (rs.next()) {
						tablerowcount = rs.getInt(1);
					}
				} catch (Exception e) {
                      e.printStackTrace();
				} finally {
					MssqlSon.closeDB();// 关闭数据库的连接
				}
				return tablerowcount;
			}
		} else {
			JOptionPane.showMessageDialog(null, "表名不能为空");
			return 0;
		}
	}

	/**
	 * 传入表名 得到表内所有字段名称 String []字符串数组封装
	 * 
	 * @param String
	 *            tablename
	 * @return String[]
	 */
	public static String[] getTableColumnNames(String inTableName) {
		if (inTableName != null) {
			if (inTableName.length() <= 0) {
				JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
				return null;
			} else {
				try {
					conn = MssqlSon.getConnection();
					rs = conn.createStatement().executeQuery("select name from syscolumns where id=(select id from sysobjects where xtype='u' and name='"+inTableName+"')") ;	
					int tempCount=0;
					
					while(rs.next()) {
						if(tablecolumnnames==null)
						{
							tablecolumnnames=new String[getTableColumnCount()];
						}
						tablecolumnnames[tempCount] = rs.getString(1);
						tempCount++;
						System.out.println(tempCount+"||count");
					}
				} catch (Exception e) {
                     e.printStackTrace();
				}
				return tablecolumnnames;
			}
		} else {
			JOptionPane.showMessageDialog(null, "表名不能为空");
			return null;
		}
	}

	/**
	 * 得到表内所有字段名称 String []字符串数组封装 使用前请先调用setTableName方法
	 * 
	 * @return String[]
	 */
	public static String[] getTableColumnNames() {
		if (tablename != null) {
			if (tablename.length() <= 0) {
				JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
				return null;
			} else {
				try {
					conn = MssqlSon.getConnection();
					rs = conn.createStatement().executeQuery("select * from syscolumns where id=(select id from sysobjects where xtype='u' and name='"+tablename+"')") ;	
					int tempCount=0;
					tablecolumnnames=new String[getTableColumnCount()];
					while(rs.next()) {
						tablecolumnnames[tempCount] = rs.getString("name");
						tempCount++;
					}
				} catch (Exception e) {
                    e.printStackTrace();
				} 
				return tablecolumnnames;
			}
		} else {
			JOptionPane.showMessageDialog(null, "表名不能为空");
			return null;
		}
	}

	/**
	 * 传入表名列名 得到本列所有数据 封装成字符串数组
	 * 
	 * @param String
	 *            tablename
	 * @param String
	 *            columnname
	 * @return String[] tablerowdatas
	 */
	public static String[] getTableColumnDatas(String tablename,String columnname) {
		if (tablename == null || columnname == null) {
			JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
			return null;
		} else {
			try {
				int i = 0;
				tablerowdatas = new String[MssqlSon.getTableRowCount(tablename)];
				conn = MssqlSon.getConnection();
				pt = conn.prepareStatement("select " + columnname + " from "
						+ tablename);
				rs = pt.executeQuery();
				while (rs.next()) {
					tablerowdatas[i] = rs.getString(columnname);
					i++;
				}
			} catch (Exception e) {
				e.printStackTrace();

			} finally {
				MssqlSon.closeDB();// 关闭数据库的连接
			}
			return tablerowdatas;
		}
	}

	/**
	 * 传入列名 得到本列所有数据 封装成字符串数组 使用前请调用setTableName()方法
	 * 
	 * @param String
	 *            columnname
	 * @return String[] tablerowdatas
	 */
	public static String[] getTableColumnDatas(String columnname) {
		if (tablename == null || columnname == null) {
			JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
			return null;
		} else {
			try {
				int i = 0;
				tablerowdatas = new String[MssqlSon.getTableRowCount(tablename)];
				conn = MssqlSon.getConnection();
				pt = conn.prepareStatement("select " + columnname + " from "
						+ tablename);
				rs = pt.executeQuery();
				while (rs.next()) {
					tablerowdatas[i] = rs.getString(columnname);
					i++;
				}
			} catch (Exception e) {
				e.printStackTrace();

			} finally {
				MssqlSon.closeDB();// 关闭数据库的连接
			}
			return tablerowdatas;
		}
	}

	/**
	 * 传入列名 得到本列所有数据 封装成字符串数组 使用前请调用setTableName()方法 并调用setcolumnname()方法
	 * 
	 * @return String[] tablerowdatas
	 */
	public static String[] getTableColumnDatas() {
		if (tablename == null || columnname == null) {
			JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
			return null;
		} else {
			try {
				int i = 0;
				tablerowdatas = new String[MssqlSon.getTableRowCount(tablename)];
				conn = MssqlSon.getConnection();
				pt = conn.prepareStatement("select " + columnname + " from "
						+ tablename);
				rs = pt.executeQuery();
				while (rs.next()) {
					tablerowdatas[i] = rs.getString(columnname);
					i++;
				}
			} catch (Exception e) {
				e.printStackTrace();

			} finally {
				MssqlSon.closeDB();// 关闭数据库的连接
			}
			return tablerowdatas;
		}
	}

	/**
	 * 传入表名 得到表内字段名称和字段类型
	 * 
	 * @param String
	 *            tablename
	 * @return Hashtable ht
	 */
	public static Hashtable getTableColumnNamesAndTypes(String tablename) {
		Hashtable ht = new Hashtable();
		if (tablename == null) {
			JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
			return null;
		} else {
			try {
				conn = MssqlSon.getConnection();
				pt = conn.prepareStatement("select * from " + tablename);
				rs = pt.executeQuery();
				if (rs != null) {
					rm = rs.getMetaData();
					for (int i = 1; i <= rm.getColumnCount(); i++) {
						ht.put(rm.getColumnName(i), gettypeNumTosqlStr(rm
								.getColumnType(i)));
					}
				}
			} catch (Exception e) {
                   e.printStackTrace();
			} finally {
				MssqlSon.closeDB();// 关闭数据库的连接
			}
			return ht;
		}
	}

	/**
	 * 得到表内字段名称和字段类型 使用前请调用setTableName()方法
	 * 
	 * @return Hashtable ht
	 */
	public static Hashtable getTableColumnNamesAndTypes() {
		Hashtable ht = new Hashtable();
		if (tablename == null) {
			JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
			return null;
		} else {
			try {
				conn = MssqlSon.getConnection();
				pt = conn.prepareStatement("select * from " + tablename);
				rs = pt.executeQuery();
				if (rs != null) {
					rm = rs.getMetaData();
					for (int i = 1; i <= rm.getColumnCount(); i++) {
						ht.put(rm.getColumnName(i), gettypeNumTosqlStr(rm
								.getColumnType(i)));
					}
				}
			} catch (Exception e) {
                   e.printStackTrace();
			} finally {
				MssqlSon.closeDB();// 关闭数据库的连接
			}
			return ht;
		}
	}

	//--------------------------------------------------------------------------
	/**
	 * 传入表名 把表内所有数据封装成一个二层Vector
	 * 
	 * @param String
	 *            tablename
	 * @return Vector vt
	 */
	public static Vector getAllRecordsInTableV(String tablename) {
		if (tablename == null) {
			JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
			return null;
		} else {
			try {
				vt = new Vector();
				conn = MssqlSon.getConnection();
				pt = conn.prepareStatement("select * from " + tablename);
				rs = pt.executeQuery();
				rm = pt.getMetaData();
				while (rs.next()) {
					Vector va = new Vector();
					for (int i = 1; i <= rm.getColumnCount(); i++) {
						va.add(rs.getString(i));
					}
					vt.add(va);
				}

			} catch (Exception e) {
                   e.printStackTrace();
			} finally {
				MssqlSon.closeDB();// 关闭数据库的连接
				return vt;

			}
		}

	}

	/**
	 * 得到表内所有数据 并封装成Vector 使用前请使用settablename方法
	 * 
	 * @return Vector
	 */
	public static Vector getAllRecordsInTableV() {
		if (tablename == null) {
			JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
			return null;
		} else {
			try {
				vt = new Vector();
				conn = MssqlSon.getConnection();
				pt = conn.prepareStatement("select * from " + tablename);
				rs = pt.executeQuery();
				rm = pt.getMetaData();
				while (rs.next()) {
					Vector va = new Vector();
					for (int i = 1; i <= rm.getColumnCount(); i++) {
						va.add(rs.getString(i));
					}
					vt.add(va);
				}

			} catch (Exception e) {
                 e.printStackTrace();
			} finally {
				MssqlSon.closeDB();// 关闭数据库的连接
				return vt;

			}
		}

	}

	/**
	 * 传入表名 封装表内数据为ArrayList
	 * 
	 * @param String
	 *            tablename
	 * @return ArrayList
	 */
	public static ArrayList getAllRecordsInTableA(String tablename) {
		if (tablename == null) {
			JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
			return null;
		} else {
			try {
				al = new ArrayList();
				conn = MssqlSon.getConnection();
				pt = conn.prepareStatement("select * from " + tablename);
				rs = pt.executeQuery();
				rm = pt.getMetaData();
				while (rs.next()) {
					ArrayList va = new ArrayList();
					for (int i = 1; i <= rm.getColumnCount(); i++) {
						va.add(rs.getString(i));
					}
					al.add(va);
				}

			} catch (Exception e) {
                   e.printStackTrace();
			} finally {
				MssqlSon.closeDB();// 关闭数据库的连接
				return al;

			}
		}

	}

	/**
	 * 得到表内所有数据 并封装成ArrayList 使用前请使用settablename方法
	 * 
	 * @return ArrayList
	 */
	public static ArrayList getAllRecordsInTableA() {
		if (tablename == null) {
			JOptionPane.showMessageDialog(null, "传入的表名不能为空啊");
			return null;
		} else {
			try {
				al = new ArrayList();
				conn = MssqlSon.getConnection();
				pt = conn.prepareStatement("select * from " + tablename);
				rs = pt.executeQuery();
				rm = pt.getMetaData();
				while (rs.next()) {
					ArrayList va = new ArrayList();
					for (int i = 1; i <= rm.getColumnCount(); i++) {
						va.add(rs.getString(i));
					}
					al.add(va);
				}

			} catch (Exception e) {
                   e.printStackTrace();
			} finally {
				MssqlSon.closeDB();// 关闭数据库的连接
				return al;

			}
		}

	}

	// ----------------------------------

	
	/**
	 * 返回生成好的javaBean字符串 使用前请调用setTableName()方法
	 * 
	 * @return String javabeanclass
	 */
	public static String getJavaBeanStrByTableName() {
		String nr = "";// 生成好的bean类字符串
		String zd = ""; // 存储字段名
		String zdz="";//存储全部字段并用,号分开(封装所有字段的构造方法)
		String zda="";//设置所有字段（封装所有字段)
		String ff = ""; // 存储方法名
		if (tablename != null) {
			try {
				conn = MssqlSon.getConnection();				
				pt = conn.prepareStatement("select * from " + tablename);
				rs = pt.executeQuery();
				if (rs != null) {
					String cname = tablename;// 类名与表名相同
					cname = (cname.charAt(0) + "").toUpperCase()
							+ cname.substring(1, cname.length());
					nr = nr + "\n public class " + cname + "{";
					rm = rs.getMetaData();
					for (int d = 1; d <= rm.getColumnCount(); d++) {
						zd = zd+ ("\n private  "+ MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)) + " "+ rm.getColumnName(d) + ";");
						zdz=zdz+","+MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)) + " "+ rm.getColumnName(d);
						zda= "\n this."+rm.getColumnName(d) + "="+ rm.getColumnName(d) + ";"+zda;
						ff = ff
								+ ("\n public  void set"
										+ rm.getColumnName(d)
										+ "("
										+ MssqlSon.gettypeNumToJavaStr(rm
												.getColumnType(d)) + " "
										+ rm.getColumnName(d) + "){\nthis."
										+ rm.getColumnName(d) + "="
										+ rm.getColumnName(d) + ";\n}");
						ff = ff
								+ ("\n public  "
										+ MssqlSon.gettypeNumToJavaStr(rm
												.getColumnType(d)) + " get"
										+ rm.getColumnName(d) + "(){\nreturn "
										+ rm.getColumnName(d) + ";\n}");

					}
					zdz=zdz.substring(1, zdz.length());
					ff=ff+"\n public  "+cname+"() \n { \n } \n";
					ff=ff+"\n public  "+cname+"("+zdz+"){"+zda+" \n}\n";
					nr = nr + zd + ff + "\n}//end class" + cname + "\n";	
					System.out.println(nr);
				}

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				MssqlSon.closeDB();
				return nr;
			}
		} else {
			JOptionPane.showMessageDialog(null, "表名不能为空!");
			return null;
		}
	}
	
	
	/**
	 * 传入表名 返回生成好的javaBean字符串带处理方法的
	 * 
	 * @param String
	 *            tablename
	 * @return String javabeanclass
	 */
	public static String getJavaBeanAndMehodStrByTableName(String tablename) {
		String tempn="";
		String nr = "";// 生成好的bean类字符串
		String ssm="private boolean flag=false;\n";//存储bean的声明.也就是声明本Bean
		String sff="";//存储生成好的服务方法
		String zd = ""; // 存储字段名
		String zdz="";//存储全部字段及类型并用,号分开(封装所有字段的构造方法)
		String zdza="";//存储所有字段用,号分开用于添加
		String zdzz="";//存储字段的值就是?
		String zda="";//设置所有字段（封装所有字段)
		String ff = ""; // 存储方法名
		String psql="";//服务方法中要执行的语句插入语句
		String psqlup="";//服务方法中要执行的更新语句
		String psqlptset="";//存储pt的set方法
		String psqlptupdateset="";//存储pt的update时的set的内容
		String psqlptupset="";//使用update时pt的
		String psqlptde="";//删除用到的sql语句
		String diyizd="";//第一个字段名
		String diyitype="";//第一个字段数据类型
		int d1=0;//存储update时where的?坐标
		if (tablename != null) {
			tempn=(tablename.charAt(0)+"").toUpperCase()+tablename.substring(1, tablename.length());			
			try {
				sff=sff+("public ArrayList getAll"+tempn+"Info(){\n return MssqlSon.getAllRecordsInTableA("+tablename+");\n}\n");
				psql="insert into "+tablename+"(";
				psqlup="update "+tablename+" set ";
				psqlptde="delete "+tablename+" where ";
				conn = MssqlSon.getConnection();				
				pt = conn.prepareStatement("select * from " + tablename);
				rs = pt.executeQuery();
				if (rs != null) {
					String cname = tablename;// 类名与表名相同
					cname = tempn;
					ssm=ssm+"private  "+cname+" "+tablename+"=null;\n";					
					nr = nr + "\n public class " + cname + "{";
					rm = rs.getMetaData();
					for (int d = 1; d <= rm.getColumnCount(); d++) {
						String typest=(MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)).charAt(0)+"").toUpperCase()+MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)).substring(1,MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)).length());
						zdzz="?,"+zdzz;
						diyizd=rm.getColumnName(1);
						diyitype=(MssqlSon.gettypeNumToJavaStr(rm.getColumnType(1)).charAt(0)+"").toUpperCase()+MssqlSon.gettypeNumToJavaStr(rm.getColumnType(1)).substring(1,MssqlSon.gettypeNumToJavaStr(rm.getColumnType(1)).length());
						psqlptupdateset=psqlptupdateset+rm.getColumnName(d)+"=?,";
						psqlptset=psqlptset+"pt.set"+typest+"("+d+","+tablename+".get"+rm.getColumnName(d)+"());\n";					
						zd = zd+ ("\n private  "+ MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)) + " "+ rm.getColumnName(d) + ";");
						zdza=zdza+","+rm.getColumnName(d);
						zdz=zdz+","+MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)) + " "+ rm.getColumnName(d);
						zda= "\n this."+rm.getColumnName(d) + "="+ rm.getColumnName(d) + ";"+zda;
						ff = ff
								+ ("\n public  void set"
										+ rm.getColumnName(d)
										+ "("
										+ MssqlSon.gettypeNumToJavaStr(rm
												.getColumnType(d)) + " "
										+ rm.getColumnName(d) + "){\nthis."
										+ rm.getColumnName(d) + "="
										+ rm.getColumnName(d) + ";\n}");
						ff = ff
								+ ("\n public "
										+ MssqlSon.gettypeNumToJavaStr(rm
												.getColumnType(d)) + " get"
										+ rm.getColumnName(d) + "(){\nreturn "
										+ rm.getColumnName(d) + ";\n}");

					}
					zdz=zdz.substring(1, zdz.length());
					zdza=zdza.substring(1,zdza.length());
					psqlptupdateset=psqlptupdateset.substring(0, psqlptupdateset.length()-1)+" where "+diyizd+"=?";
					psql="insert into "+tablename+"("+zdza+") values("+zdzz.substring(0,zdzz.length()-1)+")";
					psqlup=psqlup+psqlptupdateset;
					psqlptde=psqlptde+diyizd+"=?";
					psqlptupset=psqlptset+"pt.set"+diyitype+"("+(rm.getColumnCount()+1)+","+tablename+".get"+diyizd+"());\n";
					
					sff=sff+("\npublic boolean add"+tempn+"info("+tempn+" "+tablename+"){\n"+"conn=com.vsked.models.ConnectDB.getConnectionJDBC();\n try {\n pt=conn.prepareStatement("+'"'+psql+'"'+");\n"+psqlptset+"int row=pt.executeUpdate();\n if(row>0){ \n flag=true;\n  }\n } catch (SQLException e) {\n e.printStackTrace();\n }finally{\n com.vsked.models.ConnectDB.close(); \n }\n return flag;\n}");
					sff=sff+("\npublic boolean update"+tempn+"info("+tempn+" "+tablename+"){\n"+"conn=com.vsked.models.ConnectDB.getConnectionJDBC();\n try {\n pt=conn.prepareStatement("+'"'+psqlup+'"'+");\n"+psqlptupset+"int row=pt.executeUpdate();\n if(row>0){ \n flag=true;\n  }\n } catch (SQLException e) {\n e.printStackTrace();\n }finally{\n com.vsked.models.ConnectDB.close(); \n }\n return flag;\n}");
					sff=sff+("\npublic boolean delete"+tempn+"info("+tempn+" "+tablename+"){\n"+"conn=com.vsked.models.ConnectDB.getConnectionJDBC();\n try {\n pt=conn.prepareStatement("+'"'+psqlptde+'"'+");\n"+"pt.set"+diyitype+"("+1+","+tablename+".get"+diyizd+"());\n"+"int row=pt.executeUpdate();\n if(row>0){ \n flag=true;\n  }\n } catch (SQLException e) {\n e.printStackTrace();\n }finally{\n com.vsked.models.ConnectDB.close(); \n }\n return flag;\n}");
					System.out.println(sff);
					ff=ff+"\n public  "+cname+"() \n { \n } \n";
					ff=ff+"\n public  "+cname+"("+zdz+"){"+zda+" \n}\n";
					nr = nr + zd + ff +sff+ "\n}//end class" + cname + "\n";	
					System.out.println("---------------------------------");
					System.out.println(nr);
				}

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				MssqlSon.closeDB();
				return nr;
			}
		} else {
			JOptionPane.showMessageDialog(null, "表名不能为空!");
			return null;
		}

	}
	//----------------------
	/**
	 * 返回生成好的javabean带增加删除修改的方法哟...呵呵
	 */
	public static String getJavaBeanAndMehodStrByTableNameTest(String tablename) {
		String tempn="";
		String nr = "";// 生成好的bean类字符串
		String ssm="private boolean flag=false;\n";//存储bean的声明.也就是声明本Bean
		String sff="";//存储生成好的服务方法
		String zd = ""; // 存储字段名
		String zdz="";//存储全部字段及类型并用,号分开(封装所有字段的构造方法)
		String zdza="";//存储所有字段用,号分开用于添加
		String zdzz="";//存储字段的值就是?
		String zda="";//设置所有字段（封装所有字段)
		String ff = ""; // 存储方法名
		String psql="";//服务方法中要执行的语句插入语句
		String psqlup="";//服务方法中要执行的更新语句
		String psqlptset="";//存储pt的set方法
		String psqlptupdateset="";//存储pt的update时的set的内容
		String psqlptupset="";//使用update时pt的
		String psqlptde="";//删除用到的sql语句
		String diyizd="";//第一个字段名
		String diyitype="";//第一个字段数据类型
		//int d1=0;//存储update时where的?坐标
		String psqlptse="";//存储查询时用到的sql语句
		String psqlptseset="";//存储查询时用到的rs的get方法
		
		if (tablename != null) {
			tempn=(tablename.charAt(0)+"").toUpperCase()+tablename.substring(1, tablename.length());			
			try {
				//sff=sff+("public ArrayList getAll"+tempn+"Info(){\n return MssqlSon.getAllRecordsInTableA("+tablename+");\n}\n");
				psql="insert into "+tablename+"(";
				psqlup="update "+tablename+" set ";
				psqlptde="delete "+tablename+" where ";
				psqlptse="select * from "+tablename;
				conn = MssqlSon.getConnection();				
				pt = conn.prepareStatement("select * from " + tablename);
				rs = pt.executeQuery();
				if (rs != null) {
					String cname = tablename;// 类名与表名相同
					cname = tempn;
					ssm=ssm+"private  "+cname+" "+tablename+"=null;\n";					
					nr = nr + "\n public class " + cname + "{";
					rm = rs.getMetaData();
					for (int d = 1; d <= rm.getColumnCount(); d++) {
						String typest=(MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)).charAt(0)+"").toUpperCase()+MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)).substring(1,MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)).length());
						zdzz="?,"+zdzz;
						diyizd=rm.getColumnName(1);
						diyitype=(MssqlSon.gettypeNumToJavaStr(rm.getColumnType(1)).charAt(0)+"").toUpperCase()+MssqlSon.gettypeNumToJavaStr(rm.getColumnType(1)).substring(1,MssqlSon.gettypeNumToJavaStr(rm.getColumnType(1)).length());
						psqlptupdateset=psqlptupdateset+rm.getColumnName(d)+"=?,";
						psqlptset=psqlptset+"pt.set"+typest+"("+d+","+tablename+".get"+rm.getColumnName(d)+"());\n";
						psqlptseset=psqlptseset+",rs.get"+typest+"("+d+")";
						zd = zd+ ("\n private  "+ MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)) + " "+ rm.getColumnName(d) + ";");
						zdza=zdza+","+rm.getColumnName(d);
						zdz=zdz+","+MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)) + " "+ rm.getColumnName(d);
						zda= "\n this."+rm.getColumnName(d) + "="+ rm.getColumnName(d) + ";"+zda;
						ff = ff
								+ ("\n public  void set"
										+ rm.getColumnName(d)
										+ "("
										+ MssqlSon.gettypeNumToJavaStr(rm
												.getColumnType(d)) + " "
										+ rm.getColumnName(d) + "){\nthis."
										+ rm.getColumnName(d) + "="
										+ rm.getColumnName(d) + ";\n}");
						ff = ff
								+ ("\n public "
										+ MssqlSon.gettypeNumToJavaStr(rm
												.getColumnType(d)) + " get"
										+ rm.getColumnName(d) + "(){\nreturn "
										+ rm.getColumnName(d) + ";\n}");

					}
					zdz=zdz.substring(1, zdz.length());
					zdza=zdza.substring(1,zdza.length());
					psqlptupdateset=psqlptupdateset.substring(0, psqlptupdateset.length()-1)+" where "+diyizd+"=?";
					psql="insert into "+tablename+"("+zdza+") values("+zdzz.substring(0,zdzz.length()-1)+")";
					psqlup=psqlup+psqlptupdateset;
					psqlptde=psqlptde+diyizd+"=?";
					psqlptupset=psqlptset+"pt.set"+diyitype+"("+(rm.getColumnCount()+1)+","+tablename+".get"+diyizd+"());\n";
					psqlptseset=psqlptseset.substring(1,psqlptseset.length());
					System.out.println(psqlptseset);
					sff=sff+("\npublic boolean add"+tempn+"info("+tempn+" "+tablename+"){\n"+"conn=com.vsked.models.ConnectDB.getConnectionJDBC();\n try {\n pt=conn.prepareStatement("+'"'+psql+'"'+");\n"+psqlptset+"int row=pt.executeUpdate();\n if(row>0){ \n flag=true;\n  }\n } catch (SQLException e) {\n e.printStackTrace();\n }finally{\n com.vsked.models.ConnectDB.close(); \n }\n return flag;\n}");
					sff=sff+("\npublic boolean update"+tempn+"info("+tempn+" "+tablename+"){\n"+"conn=com.vsked.models.ConnectDB.getConnectionJDBC();\n try {\n pt=conn.prepareStatement("+'"'+psqlup+'"'+");\n"+psqlptupset+"int row=pt.executeUpdate();\n if(row>0){ \n flag=true;\n  }\n } catch (SQLException e) {\n e.printStackTrace();\n }finally{\n com.vsked.models.ConnectDB.close(); \n }\n return flag;\n}");
					sff=sff+("\npublic boolean delete"+tempn+"info("+tempn+" "+tablename+"){\n"+"conn=com.vsked.models.ConnectDB.getConnectionJDBC();\n try {\n pt=conn.prepareStatement("+'"'+psqlptde+'"'+");\n"+"pt.set"+diyitype+"("+1+","+tablename+".get"+diyizd+"());\n"+"int row=pt.executeUpdate();\n if(row>0){ \n flag=true;\n  }\n } catch (SQLException e) {\n e.printStackTrace();\n }finally{\n com.vsked.models.ConnectDB.close(); \n }\n return flag;\n}");
					sff=sff+("\npublic ArrayList getAll"+tempn+"info(){\n"+"al = new ArrayList();\nconn=com.vsked.models.ConnectDB.getConnectionJDBC();\n try {\n pt=conn.prepareStatement("+'"'+psqlptse+'"'+");\n"+"rs = pt.executeQuery();\n while (rs.next()) { \n "+tablename+"=new "+tempn+"("+psqlptseset+")\n al.add("+tablename+");\n }\n } catch (SQLException e) {\n e.printStackTrace();\n }finally{\n com.vsked.models.ConnectDB.close(); \n }\n return al;\n}");
					System.out.println(sff);
					ff=ff+"\n public  "+cname+"() \n { \n } \n";
					ff=ff+"\n public  "+cname+"("+zdz+"){"+zda+" \n}\n";
					nr = nr + zd + ff +sff+ "\n}//end class" + cname + "\n";	
					System.out.println("---------------------------------");
					System.out.println(nr);
				}

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				MssqlSon.closeDB();
				return nr;
			}
		} else {
			JOptionPane.showMessageDialog(null, "表名不能为空!");
			return null;
		}

	}

	//------------------------

	public static String getJavaBeanStrByTableName(String tablename) {
		String nr = "";// 生成好的bean类字符串
		String zd = ""; // 存储字段名
		String zdz="";//存储全部字段并用,号分开(封装所有字段的构造方法)
		String zda="";//设置所有字段（封装所有字段)
		String ff = ""; // 存储方法名
		if (tablename != null) {
			try {
				conn = MssqlSon.getConnection();				
				pt = conn.prepareStatement("select * from " + tablename);
				rs = pt.executeQuery();
				if (rs != null) {
					String cname = tablename;// 类名与表名相同
					cname = (cname.charAt(0) + "").toUpperCase()
							+ cname.substring(1, cname.length());
					nr = nr + "\n public class " + cname + "{";
					rm = rs.getMetaData();
					for (int d = 1; d <= rm.getColumnCount(); d++) {
						zd = zd+ ("\n private  "+ MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)) + " "+ rm.getColumnName(d) + ";");
						zdz=zdz+","+MssqlSon.gettypeNumToJavaStr(rm.getColumnType(d)) + " "+ rm.getColumnName(d);
						zda= "\n this."+rm.getColumnName(d) + "="+ rm.getColumnName(d) + ";"+zda;
						ff = ff
								+ ("\n public  void set"
										+ rm.getColumnName(d)
										+ "("
										+ MssqlSon.gettypeNumToJavaStr(rm
												.getColumnType(d)) + " "
										+ rm.getColumnName(d) + "){\nthis."
										+ rm.getColumnName(d) + "="
										+ rm.getColumnName(d) + ";\n}");
						ff = ff
								+ ("\n public  "
										+ MssqlSon.gettypeNumToJavaStr(rm
												.getColumnType(d)) + " get"
										+ rm.getColumnName(d) + "(){\nreturn "
										+ rm.getColumnName(d) + ";\n}");

					}
					zdz=zdz.substring(1, zdz.length());
					ff=ff+"\n public  "+cname+"() \n { \n } \n";
					ff=ff+"\n public  "+cname+"("+zdz+"){"+zda+" \n}\n";
					nr = nr + zd + ff + "\n}//end class" + cname + "\n";					
				}

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				MssqlSon.closeDB();
				return nr;
			}
		} else {
			JOptionPane.showMessageDialog(null, "表名不能为空!");
			return null;
		}

	}

	/**
	 * 保存为一个文件 使用前请使用setTablename方法
	 */
	public static void saveJavaBeanFileByTableName(){
		if(tablename.length()>0){
		MssqlSon.saveFile((MssqlSon.getJavaBeanStrByTableName(tablename)));
		}else{
			JOptionPane.showMessageDialog(null, "传入的表名不能为空!");
		}
	}
	
	/**
	 * 传入表名 保存为一个文件
	 * @param String tablename
	 */
	public static void saveJavaBeanFileByTableName(String tablename){
		if(tablename.length()>0){
		MssqlSon.saveFile((MssqlSon.getJavaBeanStrByTableName(tablename)));
		}else{
			JOptionPane.showMessageDialog(null, "传入的表名不能为空!");
		}
	}
	
	/**
	 * 传入表名得到javaBean带增加删除修改方法的javabean哟
	 * @param String tablename
	 */
	public static void saveJavaBeanAndMethodFileByTableName(String tablename){		
		if(tablename.length()>0){
		MssqlSon.saveFile((MssqlSon.getJavaBeanAndMehodStrByTableNameTest(tablename)));
		}else{
			JOptionPane.showMessageDialog(null, "传入的表名不能为空!");
		}
	}
	
	
	
	/**
	 * 传入字符串 写到文件
	 * @param String str
	 */
	public static void saveFile(String str)
	{
		JFileChooser jfch = new JFileChooser();
		int x = jfch.showSaveDialog(null);
		if(x==0)
		{
			String fpath = jfch.getSelectedFile().getAbsolutePath();			
			try {
				File f = new File(fpath);
				FileWriter fw = new FileWriter(f,true);
				fw.write(str);
				fw.close();				
		    }
		    catch (Exception ex) {
		    	System.out.println (ex.getMessage());
		    }			
		}//end if
	}//end save file

	// ------------------------------------------------------------------

	/**
	 * 传入一个数字 返回这个数字代表的sql数据类型
	 * 
	 * @param int n
	 * @return String typename
	 */
	public static String gettypeNumTosqlStr(int n) {
		String t = "varchar";
		switch (n) {
		case -7:
			t = "bit";
			break;
		case -6:
			t = "tinyint";
			break;
		case -5:
			t = "bigint";
			break;
		case -2:
			t = "binary";
			break;
		case 1:
			t = "char";
			break;
		case 3:
			t = "money";
			break;
		case 4:
			t = "int";
			break;
		case 5:
			t = "smallint";
			break;
		case 6:
			t = "float";
			break;
		case 12:
			t = "varchar";
			break;
		case 93:
			t = "datetime";
			break;
		default:
			t = "varchar";
			break;
		}
		return t;
	}

	/**
	 * 传入一个数字 返回这个sql数字代表的Java数据类型
	 * 
	 * @param int n
	 * @return String typename
	 */
	public static String gettypeNumToJavaStr(int n) {
		String t = "String";
		switch (n) {
		case -5:
			t = "int";
			break;
		case 1:
			t = "char";
			break;
		case 3:
			t = "float";
			break;
		case 4:
			t = "int";
			break;
		case 6:
			t = "float";
			break;
		case 12:
			t = "String";
			break;
		case 93:
			t = "String";
			break;
		default:
			t = "String";
			break;
		}
		return t;
	}

	// ------------------------------------------------------------------------
	/**
	 * 关闭数据库方法
	 */
	public static void closeDB() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (st != null) {
				st.close();
			}
			if (pt != null) {
				pt.close();
			}
			if (ct != null) {
				ct.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
	

}// end class MssqlSon
